﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

using System.Data.SqlClient;

namespace FieldDevice.Power
{
    class PowerStorage : Component
    {
        static PowerStorage powerstorage;
        public static PowerStorage StaticPowerStorage
        {
            get
            {
                if (powerstorage == null)
                    powerstorage = new PowerStorage();
                return powerstorage;
            }
        }
        SqlConnection Connection = new SqlConnection(FieldLib.SystemConfig.GlobalSystemConfig.ConnectionString);
        System.Data.SqlClient.SqlCommand command;
        public void RecordPowerReads(Device device, PowerQueue.PowerItem record)
        {
                if (command == null)
                {
                    command = new System.Data.SqlClient.SqlCommand();
                    command.CommandText = "RecordPowerRead";
                    //command.CommandText = "INSERT INTO PowerReads(NCPAddress, DeviceName, ReadTime, TimeSpan, PRead, PComsuption, QRead, QComsuption, TotalComsuption, H) VALUES(@NCPAddress, @DeviceName, @ReadTime, @TimeSpan, @PRead, @PComsuption, @QRead, @QComsuption, @TotalComsuption, @H)";
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.Connection = this.Connection;
                    command.Parameters.Add("@NCPAddress", System.Data.SqlDbType.Int);
                    command.Parameters.Add("@DeviceName", System.Data.SqlDbType.NChar);
                    command.Parameters.Add("@ReadTime", System.Data.SqlDbType.DateTime);
                    command.Parameters.Add("@TimeSpan", System.Data.SqlDbType.Int);

                    command.Parameters.Add("@PRead", System.Data.SqlDbType.Float);
                    command.Parameters.Add("@PComsuption", System.Data.SqlDbType.Float);
                    command.Parameters.Add("@QRead", System.Data.SqlDbType.Float);
                    command.Parameters.Add("@QComsuption", System.Data.SqlDbType.Float);
                    command.Parameters.Add("@TotalComsuption", System.Data.SqlDbType.Float);
                    command.Parameters.Add("@H", System.Data.SqlDbType.Float);
                    //command.Parameters.Add("@Request", System.Data.SqlDbType.Binary);
                    //command.Parameters.Add("@Response", System.Data.SqlDbType.VarBinary);

                }
                lock (command)
                {
                    command.Parameters["@NCPAddress"].Value = device.NCPAddress;
                    command.Parameters["@DeviceName"].Value = device.DeviceName;
                    command.Parameters["@ReadTime"].Value = record.Time;
                    command.Parameters["@TimeSpan"].Value = record.TimeSpan.Seconds;
                    command.Parameters["@PRead"].Value = record.PP;
                    command.Parameters["@PComsuption"].Value = record.PComsuption;
                    command.Parameters["@QRead"].Value = record.QP;
                    command.Parameters["@QComsuption"].Value = record.QComsuption;
                    command.Parameters["@TotalComsuption"].Value = record.TotalComsuption;
                    command.Parameters["@H"].Value = record.PF;
                    //command.Parameters["@Request"].Value = record.Request;
                    //command.Parameters["@Response"].Value = record.Response;
                    try
                    {
                        command.Connection.Open();
                        command.ExecuteNonQuery();
                        command.Connection.Close();
                    }
                    catch (Exception exp)
                    {
                        throw exp;
                    }
                    finally
                    {
                        if (command.Connection.State == System.Data.ConnectionState.Open)
                            command.Connection.Close();
                    }
                }
        }
        System.Data.SqlClient.SqlCommand readcommand;
        public void RecordFieldRead(Device device, string readname, float readvalue)
        {
            if (readcommand == null)
            {
                readcommand = new System.Data.SqlClient.SqlCommand();
                readcommand.CommandText = "RecordFieldRead";
                readcommand.CommandType = System.Data.CommandType.StoredProcedure;
                readcommand.Connection = this.Connection;
                readcommand.Parameters.Add("@NCPAddress", System.Data.SqlDbType.Int);
                readcommand.Parameters.Add("@ReadName", System.Data.SqlDbType.NChar);
                readcommand.Parameters.Add("@ReadValue", System.Data.SqlDbType.Float);

            }
            lock (command)
            {
                readcommand.Parameters["@NCPAddress"].Value = device.NCPAddress;
                readcommand.Parameters["@ReadName"].Value = readname;
                readcommand.Parameters["@ReadValue"].Value = readvalue;
                //readcommand.Parameters["@Request"].Value = record.Request;
                //readcommand.Parameters["@Response"].Value = record.Response;
                try
                {
                    readcommand.Connection.Open();
                    readcommand.ExecuteNonQuery();
                    readcommand.Connection.Close();
                }
                catch (Exception exp)
                {
                    throw exp;
                }
                finally
                {
                    if (readcommand.Connection.State == System.Data.ConnectionState.Open)
                        readcommand.Connection.Close();
                }
            }
        }

        public DsFieldRead.FieldReadsDataTable ReadValues(int ncpaddress)
        {
            DsFieldReadTableAdapters.FieldReadsTableAdapter adapter = new DsFieldReadTableAdapters.FieldReadsTableAdapter();
            DsFieldRead.FieldReadsDataTable datatable = new DsFieldRead.FieldReadsDataTable();
            adapter.Fill(datatable);
            return datatable;
        }
    }
}
